<?php
namespace App\Http\Controllers;


use App\Libs\Common;
use App\Libs\Curl;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;


class TrackController extends Controller
{
    public function index()
    {
        echo '17track';exit;
    }

    public function register()
    {
        echo 'register';exit;


    }

    public function notify($data=null)
    {
        $path = storage_path().'/logs/17track_notify.log';
        $json = file_get_contents('php://input');
        file_put_contents($path,date('Y/m/d H:i:s')."--------data: ".PHP_EOL.$json.PHP_EOL,FILE_APPEND);

        if ( !empty($json) ){
            $arr = json_decode($json);
            $latest_status_status  = $arr->data->track_info->latest_status->status;

            if ( empty($latest_status_status) ) exit;
            $num = $arr->data->number;

            if ($latest_status_status == 'Delivered'){
                $date = $arr->data->track_info->latest_event->time_raw->date;
                $time = $arr->data->track_info->latest_event->time_raw->time;
                $Delivered_time = $date.' '.$time;
                $res = DB::table('send_sample_products')->where('courier_number','=',$num)->update(['receipted_at'=>$Delivered_time,'status_17api'=>$latest_status_status]);
                exit;
            }


            if ( in_array($latest_status_status,['Expired','DeliveryFailure','Exception']) ){
                $res = DB::table('send_sample_products')->where('courier_number','=',$num)->update(['status_17api'=>'异常']);
                exit;
            }

            exit;

//        $str = date('Y/m/d H:i:s')."  data: ".PHP_EOL.$json.PHP_EOL;
//        $str = date('Y/m/d H:i:s')."data1: ".PHP_EOL.json_encode($data,JSON_UNESCAPED_UNICODE).PHP_EOL;
        }

//        $res = file_put_contents($path,date('Y/m/d H:i:s')."data1: ".PHP_EOL.json_encode($str,JSON_UNESCAPED_UNICODE).PHP_EOL,FILE_APPEND);
//        p($res);
//        p($path);
        exit;
        echo 'notify';exit;
    }


}
